//package com.preparation.service.impl;
//
//import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
//import com.baomidou.mybatisplus.core.metadata.IPage;
//import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
//import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
//import com.preparation.domain.PostComment;
//import com.preparation.domain.User;
//import com.preparation.mapper.PostCommentMapper;
//import com.preparation.mapper.PostMapper;
//import com.preparation.mapper.UserMapper;
//import com.preparation.service.IPostCommentService;
//import org.apache.logging.log4j.util.Strings;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.stereotype.Service;
//import java.util.Date;
//import java.util.List;
//
//@Service
//public class PostCommentServiceImpl extends ServiceImpl<PostCommentMapper, PostComment> implements IPostCommentService {
//
//    @Autowired
//    private PostCommentMapper postCommentMapper;
//
//    @Autowired
//    private PostMapper postMapper;
//
//    @Autowired
//    private UserMapper userMapper;
//
//    //添加评论
//    @Override
//    public Boolean addComment(PostComment postComment) {
//        postComment.setCreateTime(new Date());
//        boolean flag = postCommentMapper.insert(postComment) > 0;
//        //添加成功后，帖子的评论数+1
//        if (flag){
//            return postMapper.updateCommentsCount(postComment.getPostId(), 1) > 0;
//        }
//        return false;
//    }
//
//    //根据帖子id查询评论列表  优先显示置顶信息，其次按时间升序排序
//    @Override
//    public List<PostComment> getAllByPid(Integer postId) {
//        LambdaQueryWrapper<PostComment> qw = new LambdaQueryWrapper<>();
//
//        //优先置顶，随后按时间升序
//        qw.orderByDesc(PostComment::getIsSticky);
//        qw.orderByAsc(PostComment::getCreateTime);
//        qw.eq( PostComment::getPostId, postId);
//        List<PostComment> postCommentList = postCommentMapper.selectList(qw);
//
//        for (PostComment postComment : postCommentList) {
//            User user = userMapper.selectById(postComment.getReplierId());
//            postComment.setNickname(user.getNickname());
//            postComment.setRole(user.getRole());
//            postComment.setAvatar(user.getAvatar());
//        }
//        return postCommentList;
//    }
//
//    //置顶 取消置顶
//    @Override
//    public Boolean sticky(Integer id) {
//        PostComment postComment = postCommentMapper.selectById(id);
//        if (postComment.getIsSticky()==0){
//            return postCommentMapper.sticky(id, 1) > 0;
//        }else{
//            return postCommentMapper.sticky(id, 0) > 0;
//        }
//    }
//
//    //删除评论
//    public Boolean deleteById(Integer id){
//        PostComment postComment = postCommentMapper.selectById(id);
//        //删除评论
//        Boolean flag = postCommentMapper.deleteById(id) > 0;
//        if (flag){
//            //删除评论后，帖子评论数-1
//            return postMapper.updateCommentsCount(postComment.getPostId(), -1) > 0;
//        }
//        return false;
//    }
//
//    //查询用户收到的评论
//    public List<PostComment> getByUid(Integer userId){
//        return postCommentMapper.getByUid(userId);
//    }
//
//    //分页查询
//    @Override
//    public IPage<PostComment> getPage(int currentPage, int pageSize, PostComment postComment) {
//        LambdaQueryWrapper<PostComment> qw = new LambdaQueryWrapper<>();
//        /* 条件查询 */
//        qw.eq(postComment.getId()!=null && postComment.getId()>0, PostComment::getId, postComment.getId());
//        qw.eq(postComment.getPostId()!=null, PostComment::getPostId, postComment.getPostId());
//        qw.like(Strings.isNotEmpty(postComment.getContent()),PostComment::getContent, postComment.getContent());
//        if(postComment.getCreateTimeSort()!=null){
//            if (postComment.getCreateTimeSort()==0){
//                qw.orderByAsc(PostComment::getCreateTime);
//            }else{
//                qw.orderByDesc(PostComment::getCreateTime);
//            }
//        }else{
//            qw.orderByDesc(PostComment::getCreateTime);
//        }
//        IPage<PostComment> page = new Page(currentPage, pageSize);
//        //分页条件查询
//        postCommentMapper.selectPage(page, qw);
//
//        for (PostComment record : page.getRecords()) {
//            User user = userMapper.selectById(record.getReplierId());
//            record.setNickname(user.getNickname());
//            record.setRole(user.getRole());
//            record.setTitle(postMapper.selectById(record.getPostId()).getTitle());
//        }
//
//        return page;
//    }
//
//}
